package com.heima.behavior.listener;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.IApFollowBehaviorService;
import com.heima.user.dto.FollowBehaviorDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class FollowListener {

    @Autowired
    private IApFollowBehaviorService followBehaviorService;

    @KafkaListener(topics = "${topic.followBehaviorTopic}")
    public void handlerMessage(ConsumerRecord<String, String> record){
        try {
            if (StringUtils.isNotBlank(record.value())) {
                log.info("【行为微服务监听器】：" + record.value());
                FollowBehaviorDto followBehaviorDto = JSON.parseObject(record.value(), FollowBehaviorDto.class);
                followBehaviorService.saveFollowBeahavior(followBehaviorDto);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("关注行为保存异常：{}", e);
        }
    }
}
